/****************************************************************************
**
****************************************************************************/

#include <QSettings>

#include "util.h"
#include "baseConfigure.h"

/***************************************************基础配置***************************************************/

QList<QString> BaseConfigure::gun_number_ = {"FFFFFFFFFFFFFFFFFF", "FFFFFFFFFFFFFFFFFF"};
QString BaseConfigure::pile_model_;
QString BaseConfigure::pile_vendor_;
QString BaseConfigure::qrcode_prefix_;
QString BaseConfigure::qrcode_post_;
QString BaseConfigure::net_ip_;
quint16 BaseConfigure::net_port_;
QString BaseConfigure::net_type_;
QString BaseConfigure::net_protocol_;

/***************************************************功能配置***************************************************/

bool BaseConfigure::offline_hci_charge_;
bool BaseConfigure::offline_card_charge_;
bool BaseConfigure::online_vin_charge_;

/***************************************************系统配置***************************************************/
QString BaseConfigure::model_type_;
QList<qint8> BaseConfigure::model_count_ = {3, 3};
qint32 BaseConfigure::meter_baud_;
QString BaseConfigure::lock_type_;
QString BaseConfigure::lock_fb_;

/***************************************************充电配置***************************************************/

QList<bool> BaseConfigure::apu24_ = {false, false};
QList<qint16> BaseConfigure::power_ctrl_ = {120, 120};

/*************************************************************************************************************/

BaseConfigure::BaseConfigure(QObject *parent) : QObject(parent)
{
    QSettings settings("./config.ini", QSettings::IniFormat);

    gun_number_.replace(kGunNumberA, settings.value("settings/gunanumber", "FFFFFFFFFFFFFF").toString());
    gun_number_.replace(kGunNumberB, settings.value("settings/gunbnumber", "FFFFFFFFFFFFFF").toString());
    if ("FFFFFFFFFFFFFF" == gun_number_.at(kGunNumberA).mid(0, 14) || "FFFFFFFFFFFFFF" == gun_number_.at(kGunNumberB).mid(0, 14)) {
        if ("TaiXiangChong" == net_protocol_) {
            gun_number_.replace(kGunNumberA, "FFFFFFFFFFFFFFA");
            gun_number_.replace(kGunNumberB, "FFFFFFFFFFFFFFB");
        } else if ("YunKuaiChong" == net_protocol_) {
            gun_number_.replace(kGunNumberA, "FFFFFFFFFFFFFF01");
            gun_number_.replace(kGunNumberB, "FFFFFFFFFFFFFF02");
        } else if ("OCPP" == net_protocol_) {
            gun_number_.replace(kGunNumberA, "FFFFFFFFFFFFFF1");
            gun_number_.replace(kGunNumberB, "FFFFFFFFFFFFFF2");
        }
    }
    switch (IOV_PROTOCOL_TYPE) {
    case KIovProtolTypeTaixiangchong:
        pile_model_ = settings.value("settings/pile_model", "FFFFFFFFFFFFFF").toString();
        pile_vendor_ = settings.value("settings/pile_vendor", "Thaisen").toString();
        qrcode_prefix_ = settings.value("settings/qrcodeprefix", "").toString();
        qrcode_post_ = settings.value("settings/qrcodepost", "").toString();
        net_ip_ = settings.value("settings/net_ip", "139.198.189.72").toString();
        net_port_ = settings.value("settings/net_port", 8003).toInt();
        net_type_ = settings.value("settings/net_type", "4G").toString();
        net_protocol_ = settings.value("settings/net_protocol", "TaiXiangChong").toString();
        break;
    case KIovProtolTypeYunkuaichong:
        pile_model_ = settings.value("settings/pile_model", "FFFFFFFFFFFFFF").toString();
        pile_vendor_ = settings.value("settings/pile_vendor", "Thaisen").toString();
        qrcode_prefix_ = settings.value("settings/qrcodeprefix", "http://charge.thaisen.cn:8081").toString();
        qrcode_post_ = settings.value("settings/qrcodepost", "").toString();
        net_ip_ = settings.value("settings/net_ip", "139.198.163.108").toString();
        net_port_ = settings.value("settings/net_port", 8008).toInt();
        net_type_ = settings.value("settings/net_type", "4G").toString();
        net_protocol_ = settings.value("settings/net_protocol", "YunKuaiChong").toString();
        break;
    case KIovProtolTypeOCPP:
        pile_model_ = settings.value("settings/pile_model", "FFFFFFFFFFFFFF").toString();
        pile_vendor_ = settings.value("settings/pile_vendor", "Thaisen").toString();
        qrcode_prefix_ = settings.value("settings/qrcodeprefix", "").toString();
        qrcode_post_ = settings.value("settings/qrcodepost", "").toString();
        net_ip_ = settings.value("settings/net_ip", "wss://dowhile.run:33443/steve/websocket/CentralSystemService/").toString();
        net_port_ = settings.value("settings/net_port", 33443).toInt();
        net_type_ = settings.value("settings/net_type", "4G").toString();
        net_protocol_ = settings.value("settings/net_protocol", "OCPP").toString();
        break;
    default:
        break;
    }

    offline_hci_charge_ = settings.value("settings/offline_charging", false).toBool();

    model_type_ = settings.value("settings/model_type", "Infy").toString();
    model_count_.replace(kGunNumberA, settings.value("settings/gunamodel_count", 2).toInt());
    model_count_.replace(kGunNumberB, settings.value("settings/gunbmodel_count", 2).toInt());
    lock_type_ = settings.value("settings/lock_type", "Pulse").toString();
    lock_fb_ = settings.value("settings/lock_fb", "Normal Open").toString();
    meter_baud_ = settings.value("settings/meter_baud", 9600).toInt();

    apu24_.replace(kGunNumberA, settings.value("settings/guna24apu", false).toBool());
    apu24_.replace(kGunNumberB, settings.value("settings/gunb24apu", false).toBool());
    power_ctrl_.replace(kGunNumberA, settings.value("settings/gunamaxpower", 120).toInt());
    power_ctrl_.replace(kGunNumberB, settings.value("settings/gunbmaxpower", 120).toInt());
}

BaseConfigure::~BaseConfigure()
{

}

QString BaseConfigure::gun_number(int gunno)
{
    return gun_number_.at(gunno);
}

void BaseConfigure::set_gun_number(int gunno, const QString& gun_number)
{
    QSettings settings("./config.ini", QSettings::IniFormat);

    switch (gunno) {
    case kGunNumberA:
        settings.setValue("settings/gunanumber", gun_number);
        break;
    case kGunNumberB:
        settings.setValue("settings/gunbnumber", gun_number);
        break;
    default:
        break;
    }

    gun_number_.replace(gunno, gun_number);
}

QString BaseConfigure::pile_model()
{
    return pile_model_;
}

void BaseConfigure::set_pile_model(const QString& pile_number)
{
    QSettings settings("./config.ini", QSettings::IniFormat);

    // 创建一个非 const 的副本进行修改
    QString pile_number_copy = pile_number;  // 创建副本

    // 如果 pile_number_copy 长度不足 14，则补充零
    if (pile_number_copy.length() < 14) {
        pile_number_copy = pile_number_copy.leftJustified(14, '0');  // 在右边补充零，直到总长度为 14
    }

    // 保存设置
    settings.setValue("settings/pile_model", pile_number_copy);

    if ("TaiXiangChong" == net_protocol_) {
        set_gun_number(kGunNumberA, pile_number_copy + "A");
        set_gun_number(kGunNumberB, pile_number_copy + "B");
    } else if ("YunKuaiChong" == net_protocol_) {
        set_gun_number(kGunNumberA, pile_number_copy + "01");
        set_gun_number(kGunNumberB, pile_number_copy + "02");
    } else if ("OCPP" == net_protocol_) {
        set_gun_number(kGunNumberA, pile_number_copy + "1");
        set_gun_number(kGunNumberB, pile_number_copy + "2");
    }

    pile_model_ = pile_number_copy;
}

QString BaseConfigure::pile_vendor()
{
    return pile_vendor_;
}

void BaseConfigure::set_pile_vendor(const QString& pile_vendor)
{
    QSettings settings("./config.ini", QSettings::IniFormat);

    settings.setValue("settings/pile_vendor", pile_vendor);

    pile_vendor_ = pile_vendor;
}

QString BaseConfigure::qrcode_prefix()
{
    return qrcode_prefix_;
}

void BaseConfigure::set_qrcode_prefix(const QString& qrcode_prefix)
{
    QSettings settings("./config.ini", QSettings::IniFormat);

    settings.setValue("settings/qrcodeprefix", qrcode_prefix);

    qrcode_prefix_ = qrcode_prefix;
}

QString BaseConfigure::qrcode_post()
{
    return qrcode_post_;
}

void BaseConfigure::set_qrcode_post(const QString& qrcode_post)
{
    QSettings settings("./config.ini", QSettings::IniFormat);

    settings.setValue("settings/qrcodepost", qrcode_post);

    qrcode_post_ = qrcode_post;
}

QString BaseConfigure::net_ip()
{
    return net_ip_;
}

void BaseConfigure::set_net_ip(const QString& net_ip)
{
    QSettings settings("./config.ini", QSettings::IniFormat);

    settings.setValue("settings/net_ip", net_ip);

    net_ip_ = net_ip;
}

quint16 BaseConfigure::net_port()
{
    return net_port_;
}

void BaseConfigure::set_net_port(quint16 net_port)
{
    QSettings settings("./config.ini", QSettings::IniFormat);

    settings.setValue("settings/net_port", net_port);

    net_port_ = net_port;
}

QString BaseConfigure::net_type()
{
    return net_type_;
}

void BaseConfigure::set_net_type(const QString& net_type)
{
    QSettings settings("./config.ini", QSettings::IniFormat);

    settings.setValue("settings/net_type", net_type);

    net_type_ = net_type;
}

QString BaseConfigure::net_protocol()
{
    return net_protocol_;
}

void BaseConfigure::set_set_protocol(const QString& net_protocol)
{
    net_protocol_ = net_protocol;
}

bool BaseConfigure::offline_hci_charge()
{
    return offline_hci_charge_;
}

void BaseConfigure::set_offline_hci_charge(bool offline)
{
    QSettings settings("./config.ini", QSettings::IniFormat);

    settings.setValue("settings/offline_charging", offline);

    offline_hci_charge_ = offline;
}

QString BaseConfigure::model_type()
{
    return model_type_;
}

void BaseConfigure::set_model_type(QString type)
{
    QSettings settings("./config.ini", QSettings::IniFormat);

    settings.setValue("settings/model_type", type);

    model_type_ = type;
}

qint8 BaseConfigure::model_count(int gunno)
{
    return model_count_.at(gunno);
}

void BaseConfigure::set_model_count(int gunno, qint8 count)
{
    QSettings settings("./config.ini", QSettings::IniFormat);

    switch (gunno) {
    case kGunNumberA:
        settings.setValue("settings/gunamodel_count", count);
        break;
    case kGunNumberB:
        settings.setValue("settings/gunbmodel_count", count);
        break;
    default:
        break;
    }

    model_count_.replace(gunno, count);
}

qint32 BaseConfigure::meter_baud()
{
    return meter_baud_;
}

void BaseConfigure::set_meter_baud(qint32 baud)
{
    QSettings settings("./config.ini", QSettings::IniFormat);

    settings.setValue("settings/meter_baud", baud);

    meter_baud_ = baud;
}

QString BaseConfigure::lock_type()
{
    return lock_type_;
}

void BaseConfigure::set_lock_type(QString type)
{
    QSettings settings("./config.ini", QSettings::IniFormat);

    settings.setValue("settings/lock_type", type);

    lock_type_ = type;
}

QString BaseConfigure::lock_fb()
{
    return lock_fb_;
}

void BaseConfigure::set_lock_fb(QString fb)
{
    QSettings settings("./config.ini", QSettings::IniFormat);

    settings.setValue("settings/lock_fb", fb);

    lock_fb_ = fb;
}

bool BaseConfigure::apu24(int gunno)
{
    return apu24_.at(gunno);
}

void BaseConfigure::set_apu24(int gunno, bool is_24v)
{
    QSettings settings("./config.ini", QSettings::IniFormat);

    switch (gunno) {
    case kGunNumberA:
        settings.setValue("settings/guna24apu", is_24v);
        break;
    case kGunNumberB:
        settings.setValue("settings/gunb24apu", is_24v);
        break;
    default:
        break;
    }

    apu24_.replace(gunno, is_24v);
}

qint16 BaseConfigure::power_ctrl(int gunno)
{
    return power_ctrl_.at(gunno);
}

void BaseConfigure::set_power_ctrl(int gunno, qint16 power)
{
    QSettings settings("./config.ini", QSettings::IniFormat);

    switch (gunno) {
    case kGunNumberA:
        settings.setValue("settings/gunamaxpower", power);
        break;
    case kGunNumberB:
        settings.setValue("settings/gunbmaxpower", power);
        break;
    default:
        break;
    }

    power_ctrl_.replace(gunno, power);
}
